package com.ruoyi.web.controller.dj;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ProcessStopException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.SseEmitterUtils;
import com.ruoyi.dj.service.SolarRadiationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;

/**
 * <p>
 * </p>
 *
 * @author liuhl
 * @since 2024-06-30
 */

@RestController
@RequestMapping("/dj/dataForecast")
@Api(value = "SolarRadiationController", tags = "多模型计算与预报-太阳辐射指数预报")
public class SolarRadiationController extends BaseController {

    @Resource
    private SolarRadiationService solarRadiationService;



    //@PreAuthorize("@ss.hasPermi('dj:dataForecast:getTatestTime')")
    @ApiOperation(value = "获取太阳活动指数最新时间", notes = "太阳活动指数最新时间")
    @GetMapping("/getTatestTime")
    public AjaxResult getTatestTime(@ApiParam(value = "指数", name = "index",required = true) @RequestParam("index") String index) {
        return AjaxResult.success("获取成功",solarRadiationService.getTatestTime(index));
    }

    //@PreAuthorize("@ss.hasPermi('dj:dataForecast:getData')")
    @ApiOperation(value = "获取太阳活动指数数据", notes = "获取太阳活动指数数据")
    @GetMapping("/getData")
    public AjaxResult getData(@ApiParam(value = "指数", name = "index",required = true) @RequestParam("index") String index,@ApiParam(value = "预报方法（差分自回归移动平均模型：ARIMA，长短时记忆神经网络模型：LSTM，多元线性回归模型：LMRM）", name = "method",required = true) @RequestParam("method") String method,@ApiParam(value = "预报时间", name = "time",required = true) @RequestParam("time") String time,@ApiParam(value = "通讯ID", name = "sseId") @RequestParam(value = "sseId",required = false) String sseId) throws ProcessStopException {
        try {
            return solarRadiationService.getData(index, method, time,sseId);
        }finally {
            SseEmitterUtils.sendMessage(sseId+SecurityUtils.getLoginUser().getUsername(),"100");
            SseEmitterUtils.remove(sseId+SecurityUtils.getLoginUser().getUsername());
        }
    }

    @ApiOperation(value = "太阳辐射指数预报-评估", notes = "太阳辐射指数预报-评估")
    @GetMapping("/assess")
    public AjaxResult assess(@ApiParam(value = "指数", name = "index",required = true)
                                 @RequestParam("index") String index,
                             @ApiParam(value = "预报方法）", name = "method",required = true)
                                @RequestParam("method") String method,
                             @ApiParam(value = "预报时间", name = "time",required = true)
                                 @RequestParam("time") String time) {
        return solarRadiationService.assess(index, method, time);
    }

    //@PreAuthorize("@ss.hasPermi('dj:dataForecast:subscribe')")
    @ApiOperation(value = "建立连接", notes = "建立连接")
    @GetMapping(path = "subscribe", produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
    public SseEmitter push(String id, String flag) {
        return solarRadiationService.push(id,flag);
    }


}
